package presentacion.Vehiculo;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import negocio.Vehiculo.TransferVehiculo;

import presentacion.Controlador.Controlador;
import presentacion.Controlador.Eventos;

public class PanelVehiculo extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8202901282342150711L;
	private JButton nuevoVehiculo, eliminarVehiculo, buscarVehiculo , actualizarVehiculo;
	
	public PanelVehiculo(){
		
		this.nuevoVehiculo = new JButton("Nuevo vehiculo");
		this.eliminarVehiculo = new JButton("Eliminar vehiculo");
		this.buscarVehiculo = new JButton("Buscar vehiculo");
		this.actualizarVehiculo = new JButton("Actualizar vehiculo");
		
		//JLABELS
		final JLabel modelo = new JLabel("ID del modelo: ");
		final JLabel motor = new JLabel("ID del motor: ");
		final JLabel color = new JLabel("ID del color: ");
		final JLabel precio = new JLabel("Precio del vehiculo: ");
		final JLabel stock = new JLabel("STOCK: ");
		final JLabel lID =  new JLabel("ID del vehiculo: ");
		//JTEXTFIELDS
		final JTextField tmod = new JTextField(5);
		final JTextField tmotor = new JTextField(5);
		final JTextField tcolor = new JTextField(5);
		final JTextField tprecio = new JTextField(10);
		final JTextField tstock = new JTextField(10);
		final JTextField tID = new JTextField(20);
		//JPANELS				
		final JPanel ids = new JPanel();
		ids.setLayout(new FlowLayout());
		ids.add(modelo);
		ids.add(tmod);
		ids.add(motor);
		ids.add(tmotor);
		ids.add(color);
		ids.add(tcolor);
		
		final Controlador c = Controlador.getInstancia();	
		
		this.setLayout(new GridLayout(2,2, 2,2));
		this.add(this.nuevoVehiculo);
		this.add(this.eliminarVehiculo);
		this.add(this.buscarVehiculo);
		this.add(this.actualizarVehiculo);			

		this.nuevoVehiculo.addActionListener(new ActionListener() {					
			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				JFrame ventana = new JFrame("NUEVO VEHICULO");
				JButton boton = new JButton("CREAR");
					
				JPanel p = new JPanel();
				p.add(precio);
				p.add(tprecio);
				p.add(stock);
				p.add(tstock);
				p.add(boton);
				
				ventana.setLayout(new GridLayout(2,1));
				ventana.add(ids);
				ventana.add(p);

				ventana.pack();
				ventana.setVisible(true);
				ventana.setLocationRelativeTo(null);	
				
				boton.addActionListener(new ActionListener() {					
					@Override
					public void actionPerformed(ActionEvent arg0) {
						
						if(tmod.getText().compareToIgnoreCase("")==0)
							JOptionPane.showMessageDialog(null,"No se ha introducido el modelo");
						else if(tmotor.getText().compareToIgnoreCase("")==0)
							JOptionPane.showMessageDialog(null,"No se ha introducido el motor");
						else if(tcolor.getText().compareToIgnoreCase("")==0)
							JOptionPane.showMessageDialog(null,"No se ha introducido el color");
						else if (tstock.getText().compareToIgnoreCase("")==0)
							JOptionPane.showMessageDialog(null,"No se ha introducido el stock");
						else if (tprecio.getText().compareTo("")==0)
							JOptionPane.showMessageDialog(null,"No se ha introducido el precio");
						else{
							try{
								TransferVehiculo tVehiculo = new TransferVehiculo(Integer.parseInt(tcolor.getText()),
										  										  Integer.parseInt(tmotor.getText()),
										  										  Integer.parseInt(tmod.getText()),
										  										  Float.parseFloat(tprecio.getText()),
										  										  Integer.parseInt(tstock.getText()));
								c.accionVehiculo(Eventos.crearVehiculo , tVehiculo);
							}catch(NumberFormatException e)
							{
							JOptionPane.showMessageDialog(null,"formato de modelo,motor,color,stock y/o precio inadecuado");	
							}
						}											
					}
				});		
			}
		});
		
		this.eliminarVehiculo.addActionListener(new ActionListener() {					
			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				JFrame ventana = new JFrame("ELIMINAR VEHICULO");
				JButton boton = new JButton("ELIMINAR");
				
				ventana.setLayout(new FlowLayout());
				ventana.add(lID);
				ventana.add(tID);
				ventana.add(boton);				

				ventana.pack();
				ventana.setVisible(true);
				ventana.setLocationRelativeTo(null);	
				
				boton.addActionListener(new ActionListener() {					
					@Override
					public void actionPerformed(ActionEvent arg0) {
						if(tID.getText().compareTo("")==0)
							JOptionPane.showMessageDialog(null,"No se ha introducido el ID");
						else try{
									TransferVehiculo tVehiculo = new TransferVehiculo(Integer.parseInt(tID.getText()));
									c.accionVehiculo(Eventos.eliminarVehiculo , tVehiculo);	
							 }catch(NumberFormatException e)
							 {
								 JOptionPane.showMessageDialog(null,"formato de ID inadecuado");	
							 }									
					}
				});					
			}
		});
		
		this.buscarVehiculo.addActionListener(new ActionListener() {					
			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				JFrame ventana = new JFrame("BUSCAR VEHICULO");
				JButton boton = new JButton("BUSCAR");
				
				ventana.setLayout(new FlowLayout());
				ventana.add(lID);
				ventana.add(tID);
				ventana.add(boton);				

				ventana.pack();
				ventana.setVisible(true);
				ventana.setLocationRelativeTo(null);	
				
				boton.addActionListener(new ActionListener() {					
					@Override
					public void actionPerformed(ActionEvent arg0) {
						if(tID.getText().compareTo("")==0)
							JOptionPane.showMessageDialog(null,"No se ha introducido el ID");
						else try{
									TransferVehiculo tVehiculo = new TransferVehiculo(Integer.parseInt(tID.getText()));
									c.accionVehiculo(Eventos.buscarVehiculo , tVehiculo);	
							 }catch(NumberFormatException e)
							 {
								 JOptionPane.showMessageDialog(null,"formato de ID inadecuado");	
							 }									
					}
				});									
			}
		});
		
		this.actualizarVehiculo.addActionListener(new ActionListener() {					
			@Override
			public void actionPerformed(ActionEvent arg0) {
			
				JFrame ventana = new JFrame("ACTUALIZAR VEHICULO");
				JButton boton = new JButton("ACTUALIZAR");
					
				JPanel p = new JPanel();
				p.add(precio);
				p.add(tprecio);
				p.add(stock);
				p.add(tstock);
				p.add(boton);
				ids.add(lID);
				ids.add(tID);
				
				ventana.setLayout(new GridLayout(2,1));
				ventana.add(ids);
				ventana.add(p);

				ventana.pack();
				ventana.setVisible(true);
				ventana.setLocationRelativeTo(null);	
				
				boton.addActionListener(new ActionListener() {					
					@Override
					public void actionPerformed(ActionEvent arg0) {
						
						if(tmod.getText().compareToIgnoreCase("")==0)
							JOptionPane.showMessageDialog(null,"No se ha introducido el modelo");
						else if(tmotor.getText().compareToIgnoreCase("")==0)
							JOptionPane.showMessageDialog(null,"No se ha introducido el motor");
						else if(tcolor.getText().compareToIgnoreCase("")==0)
							JOptionPane.showMessageDialog(null,"No se ha introducido el color");
						else if (tstock.getText().compareToIgnoreCase("")==0)
							JOptionPane.showMessageDialog(null,"No se ha introducido el stock");
						else if (tprecio.getText().compareTo("")==0)
							JOptionPane.showMessageDialog(null,"No se ha introducido el precio");
						else{
							try{
								TransferVehiculo tVehiculo = new TransferVehiculo(Integer.parseInt(tcolor.getText()),
										  										  Integer.parseInt(tmotor.getText()),
										  										  Integer.parseInt(tmod.getText()),
										  										  Float.parseFloat(tprecio.getText()),
										  										  Integer.parseInt(tstock.getText()));
								c.accionVehiculo(Eventos.actualizarVehiculo , tVehiculo);
							}catch(NumberFormatException e)
							{
							JOptionPane.showMessageDialog(null,"formato de modelo,motor,color,stock y/o precio inadecuado");	
							}
						}											
					}
				});	
			}
		});	
	}	
}
